﻿<!DOCTYPE html>
<!--[if IE]><![endif]-->
<html>
  
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>How-to: Add a customized post-processor | DocFX website </title>
    <meta name="viewport" content="width=device-width">
    <meta name="title" content="How-to: Add a customized post-processor | DocFX website ">
    <meta name="generator" content="docfx 2.37.0.0">
    
    <link rel="shortcut icon" href="../favicon.ico">
    <link rel="stylesheet" href="../styles/docfx.vendor.css">
    <link rel="stylesheet" href="../styles/docfx.css">
    <link rel="stylesheet" href="../styles/main.css">
    <meta property="docfx:navrel" content="../toc.html">
    <meta property="docfx:tocrel" content="toc.html">
    
    <meta property="docfx:rel" content="../">
    
  </head>
  <body data-spy="scroll" data-target="#affix" data-offset="120">
    <div id="wrapper">
      <header>
        
        <nav id="autocollapse" class="navbar navbar-inverse ng-scope" role="navigation">
          <div class="container">
            <div class="navbar-header">
              <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
              </button>
              
              <a class="navbar-brand" href="../index.html">
                <img id="logo" class="svg" src="../logo.svg" alt="">
              </a>
            </div>
            <div class="collapse navbar-collapse" id="navbar">
              <form class="navbar-form navbar-right" role="search" id="search">
                <div class="form-group">
                  <input type="text" class="form-control" id="search-query" placeholder="Search" autocomplete="off">
                </div>
              </form>
            </div>
          </div>
        </nav>
        
        <div class="subnav navbar navbar-default">
          <div class="container hide-when-search" id="breadcrumb">
            <ul class="breadcrumb">
              <li></li>
            </ul>
          </div>
        </div>
      </header>
      <div class="container body-content">
        
        <div id="search-results">
          <div class="search-list"></div>
          <div class="sr-items">
            <p><i class="glyphicon glyphicon-refresh index-loading"></i></p>
          </div>
          <ul id="pagination"></ul>
        </div>
      </div>
      <div role="main" class="container body-content hide-when-search">
        
        <div class="sidenav hide-when-search">
          <a class="btn toc-toggle collapse" data-toggle="collapse" href="#sidetoggle" aria-expanded="false" aria-controls="sidetoggle">Show / Hide Table of Contents</a>
          <div class="sidetoggle collapse" id="sidetoggle">
            <div id="sidetoc"></div>
          </div>
        </div>
        <div class="article row grid-right">
          <div class="col-md-10">
            <article class="content wrap" id="_content" data-uid="">
<h1 id="how-to-add-a-customized-post-processor">How-to: Add a customized post-processor</h1>

<p>We provide the ability to process output files by adding a customized post-processor.
In DocFX, the index file for full-text-search is generated by one post-processor named <code>ExtractSearchIndex</code>.
In this topic, we will show how to add a customized post-processor.</p>
<h2 id="step0-preparation">Step0: Preparation</h2>
<ul>
<li>Create a new C# class library project in <code>Visual Studio</code>.</li>
<li>Add nuget packages:
<ul>
<li><a href="https://www.nuget.org/packages/System.Collections.Immutable/1.3.1"><code>System.Collections.Immutable</code></a> with version 1.3.1</li>
<li><a href="https://www.nuget.org/packages/Microsoft.Composition/1.0.31"><code>Microsoft.Composition</code></a> with version 1.0.31</li>
</ul>
</li>
<li>Add <code>Microsoft.DocAsCode.Plugins</code>
If you are building DocFX from source code, add this reference to the project,
otherwise add the nuget package <code>Microsoft.DocAsCode.Plugins</code> with the same version as DocFX.</li>
</ul>
<h2 id="step1-create-a-new-class-myprocessorcs-with-the-following-code">Step1: Create a new class (MyProcessor.cs) with the following code:</h2>
<pre><code class="lang-csharp">[Export(nameof(MyProcessor), typeof(IPostProcessor))]
public class MyProcessor : IPostProcessor
{
    // TODO: implements IPostProcessor
}
</code></pre>
<h2 id="step2-update-global-metadata">Step2: Update global metadata</h2>
<pre><code class="lang-csharp">public ImmutableDictionary&lt;string, object&gt; PrepareMetadata(ImmutableDictionary&lt;string, object&gt; metadata)
{
    // TODO: add/remove/update property from global metadata
    return metadata;
}
</code></pre>
<p>In this method, we can update the global metadata before building all the files declared in <code>docfx.json</code>. Otherwise, you can just return the metadata from parameters if you don't need to change global metadata.</p>
<p>Using <code>ExtractSearchIndex</code> for example, we add <code>&quot;_enableSearch&quot;: true</code> in global metadata. The default template would then know it should load a search box in the navbar.</p>
<h2 id="step3-process-all-the-files-generated-by-docfx">Step3: Process all the files generated by DocFX</h2>
<pre><code class="lang-csharp">    public Manifest Process(Manifest manifest, string outputFolder)
    {
        // TODO: add/remove/update all the files included in manifest
        return manifest;
    }
</code></pre>
<p>Input for the method <code>manifest</code> contains a list of all files to process, and <code>outputFolder</code> specifies the output folder where our static website will be placed. We can implement customized operations here to process all files generated by DocFX.</p>
<div class="NOTE">
<h5>Note</h5>
<p>Post-processor aims to process the output files, so the <code>FileModel</code> can't be accessed in this phase. If some metadata is needed here, an option is to save it in <code>FileModel.ManifestProperties</code> in build phase, then access it through <code>ManifestItem.Metadata</code>. Another option is to save it somewhere in output files, like HTML's <code>&lt;meta&gt;</code> Tag.</p>
</div>
<p>Using <code>ExtractSearchIndex</code> for example again, we traverse all HTML files, extract key words from these HTML files and save a file named <code>index.json</code> under the <code>outputFolder</code>. Finally we return the manifest which is not modified.</p>
<h2 id="step4-build-your-project-and-copy-the-output-dll-files-to">Step4: Build your project and copy the output dll files to:</h2>
<ul>
<li><p>Global: the folder with name <code>Plugins</code> under DocFX.exe</p>
</li>
<li><p>Non-global: the folder with name <code>Plugins</code> under a template folder, then run <code>DocFX build</code> command with parameter <code>-t {template}</code>.</p>
<p><em>Hint</em>: DocFX can merge templates, so we can specify multiple template folders as <code>DocFX build -t {templateForRender},{templateForPlugins}</code>. Each of the template folders should have a subfolder named <code>Plugins</code> with exported assemblies.</p>
</li>
</ul>
<h2 id="step5-add-your-post-processor-in-docfxjson">Step5: Add your post processor in <code>docfx.json</code></h2>
<p>In this step, we need to enable the processor by adding its name in <code>docfx.json</code>. Here is an example:</p>
<pre><code class="lang-json">{
  &quot;build&quot;: {
    ...
    &quot;postProcessors&quot;: [&quot;OutputPDF&quot;, &quot;BeautifyHTML&quot;, &quot;OutputPDF&quot;]
  }
}
</code></pre>
<p>As you can see, the <code>postProcessors</code> is an array, which means it could have multiple processors.
It needs to be pointed out that the order of <code>postProcessors</code> written in <code>docfx.json</code> is also the order to process output files.
In the above example, DocFX will run <code>OutputPDF</code> first, then <code>BeautifyHTML</code>, and then <code>OutputPDF</code> again.</p>
<p>If you want to enable the post processors without changing <code>docfx.json</code>, you can use the build command option like <code>docfx build --postProcessors=OutputPDF,BeautifyHTML,OutputPDF</code>.</p>
<p>One more thing need to be noted: the build command option <code>postProcessors</code> would override the corresponding configuration in <code>docfx.json</code>.</p>
<div id="disqus_thread"></div>
                <script>
                (function() { // DON'T EDIT BELOW THIS LINE
                var d = document, s = d.createElement('script');
                s.src = 'https://docfx-github.disqus.com/embed.js';
                s.setAttribute('data-timestamp', +new Date());
                (d.head || d.body).appendChild(s);
                })();
                </script>
                <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
            </article>
          </div>
          
          <div class="hidden-sm col-md-2" role="complementary">
            <div class="sideaffix">
              <div class="contribution">
                <ul class="nav">
                  <li>
                    <a href="#disqus_thread" class="contribution-link">0 Comments</a>
                  </li>
                </ul>
              </div>
              <nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix" id="affix">
              <!-- <p><a class="back-to-top" href="#top">Back to top</a><p> -->
              </nav>
            </div>
          </div>
        </div>
      </div>
      
      <footer>
        <div class="grad-bottom"></div>
        <div class="footer">
          <div class="container">
            <span class="pull-right">
              <a href="#top">Back to top</a>
            </span>
            <span>Copyright © 2015-2018 Microsoft<br>Generated by <strong>DocFX</strong></span>
            
          </div>
        </div>
      </footer>
    </div>
    
    <script type="text/javascript" src="../styles/docfx.vendor.js"></script>
    <script type="text/javascript" src="../styles/docfx.js"></script>
    <script type="text/javascript" src="../styles/main.js"></script>
    <script id="dsq-count-scr" src="//docfx-github.disqus.com/count.js" async=""></script>
    
    <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
    
      ga('create', 'UA-99241001-1', 'auto');
      ga('send', 'pageview');
    
    </script>
  </body>
</html>
